/*
* created by Administrator on 2018/12/7
* 好的代码像粥一样,是得用时间和精力熬的。
*/
const Koa = require('koa');
const Router = require('koa-router');
const body = require('koa-body');
const _static = require('koa-static');
let qnToken = require('./upload/qiniu.config');
let {mongoConnect,picModel} = require('./mongo');

const router = new Router();
router.get('/qiniuToken', async (ctx, next) => {
    ctx.body = {
        code: 1,
        msg: '七牛token获取成功！',
        token: qnToken
    };
    await next();
});
router.post('/savePic', async (ctx,next) => {
    let {url} = ctx.request.body;
    await picModel.create({url})
        .then(data => {
            ctx.body = {
                code: 1,
                msg: '保存成功！',
                data
            }
        })
        .catch(error => {
            ctx.body = {
                code: -1,
                msg: '保存失败！',
                error
            }
        });
    await next();
});

const app = new Koa();
app.use(body());
app.use(_static('./view/'));
app.use(router.routes());
// app.listen(8888);
mongoConnect(app);
